package command;

import command.command.Command;
import command.command.NoCommand;

import java.util.Arrays;

/**
 * @author 帅小伙
 * @date 2022/1/12
 * @description
 */
public class Controller {

    Command[] onCommands;
    Command[] offCommands;

    Command lastCommand = new NoCommand();


    public Controller() {
        onCommands = new Command[3];
        offCommands = new Command[3];
        Arrays.fill(onCommands,new NoCommand());
        Arrays.fill(offCommands,new NoCommand());
    }


    public void setCommand(int index,Command command1,Command command2){
        if(index > onCommands.length){
            System.out.println("数组越界");
            return;
        }else{
            onCommands[index] = command1;
            offCommands[index] = command2;
        }
    }


    public void on(int index){
        onCommands[index].execute();
        lastCommand = onCommands[index];
    }

    public void off(int index){
        offCommands[index].execute();
        lastCommand = onCommands[index];
    }

    public void rollBack(){
        lastCommand.undo();
    }



}
